﻿using System.Xml.Linq;
using TVersityDotNet.Responses;
using System.Globalization;
using TVersityDotNet.MediaLib;
using System;
using System.Text;

namespace TVersityDotNet.Requests
{
    public class SearchRequest : TVersityRequest
    {
        private SearchOptions _searchOptions;

        public SearchRequest(SearchOptions searchOptions)
        {
            base.Path = "medialib/search";
            searchOptions.EnsureDefaults();
            this._searchOptions = searchOptions;            
        }

        public override string RequestString
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (string key in this._searchOptions.PropertyBag.PropertiesWithValues.Keys)
                {
                    sb.AppendFormat("{0}={1}&", key, this._searchOptions.PropertyBag.PropertiesWithValues[key]);
                }

                // Remove last &
                sb.Remove(sb.Length - 1, 1);
                return string.Format(CultureInfo.InvariantCulture, "{0}?{1}", this.Path, sb.ToString());
            }
        }

        public override TVersityResponse CreateResponse(XDocument xmlDocument)
        {
            MediaLibResponse response = new MediaLibResponse(xmlDocument);
            return response;
        }
    }
}
